/**
 * 
 */
package three_star_craft.uap.timer;

import org.grlea.log.SimpleLogger;

import three_star_craft.Generator;
import three_star_craft.uap.UapSigNo;

/**
 * @author w.antosiewic
 *
 */
public class Timer
{
	private static final SimpleLogger logger = new SimpleLogger(Timer.class);
	protected Generator _generator;
	protected long _timeout;
	
	protected UapSigNo _timerSigNo;
	
	protected java.util.Timer _timer;
	
	public Timer(Generator generator, UapSigNo timerSigNo, long timeout)
	{
	    _timer = new java.util.Timer();
		_generator = generator;
		_timerSigNo = timerSigNo;
		_timeout = timeout;
	}
	
	public void start()
	{
		// TimerTask will be executed after _timeout
		_timer.schedule(new TimerTask(), _timeout);
	}
	
	public void cancel()
	{
		_timer.cancel();
	}
	
	public String toString()
	{
		return "UapTimer "+_timerSigNo;
	}
	
	// Task which is executed when this timer expires
	class TimerTask extends java.util.TimerTask
	{
		@Override
		public void run()
		{
			logger.debugObject("Timer expired:", Timer.this);
			TimerEvent e = new TimerEvent(_timerSigNo);
			_generator.handleEvent(e);
		}
		
	}
}
